/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
 //使用递归
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.empty()) return nullptr;
        auto it=find(inorder.begin(),inorder.end(),preorder[0]);
        int leftSz=it-inorder.begin();
        vector<int> pre1(preorder.begin()+1,preorder.begin()+1+leftSz);//数组的拷贝构造，左闭右开
        vector<int> pre2(preorder.begin()+1+leftSz,preorder.end());
        vector<int> in1(inorder.begin(),inorder.begin()+1+leftSz);
        vector<int> in2(inorder.begin()+1+leftSz,inorder.end());
        TreeNode* leftTree=buildTree(pre1,in1);
        TreeNode* rightTree=buildTree(pre2,in2);
        return new TreeNode(preorder[0],leftTree,rightTree);
            }
};
